#include <stdio.h>
#include <pthread.h>
#include <sched.h>
#include <assert.h>

static int GetThreadPolicyFunc(pthread_attr_t *pAttr)
{
    int iPlicy;
    int igp = pthread_attr_getschedpolicy(pAttr, &iPlicy);
    assert(igp==0);
    switch (iPlicy)
    {
    case SCHED_FIFO:
        printf("policy is --> SCHED_FIFO \n");
        break;
    case SCHED_RR:
        printf("policy is --> SCHED_RR \n");
        break;
     case SCHED_OTHER:
        printf("policy is --> SCHED_OTHER \n");
        break;
    
    default:
        printf("policy is --> unknow \n");
        break;
    }
    return iPlicy;
}

static void PrintThreadPriorityFunc(pthread_attr_t *pAttr, int iPolicy)
{
    int iPrioty = sched_get_priority_max(iPolicy);
    assert(iPolicy != -1);
    printf("Max_priority is:%d\n", iPolicy);

    iPrioty = sched_get_priority_min(iPolicy);
    assert(iPolicy != -1);
    printf("Min_priority is:%d\n", iPolicy);

}

static int GetThreadPriorityFunc(pthread_attr_t *pAttr)
{
    struct sched_param sParam;
    int irs = pthread_attr_getschedparam(pAttr, &sParam);

    assert(irs==0);
    printf("priority=%d\n", sParam.__sched_priority);
    return sParam.__sched_priority;
}

static int SetThreadPriorityFunc(pthread_attr_t *pAttr, int iPolicy)
{
    int irs =pthread_attr_setschedpolicy(pAttr, iPolicy);
    assert(irs==0);
    GetThreadPolicyFunc(pAttr);
}

static void SetThreadPolicyFunc(pthread_attr_t *pAttr, int iPolicy)
{
    int irs = pthread_attr_setschedpolicy(pAttr, iPolicy);
    assert(irs==0);
    GetThreadPolicyFunc(pAttr);
}

int main(int argc, char* argv[])
{
    pthread_attr_t pAttr;
    struct sched_param sched;
    int irs=pthread_attr_init(&pAttr);
    assert(irs==0);
    int iPlicy = GetThreadPolicyFunc(&pAttr);

    printf("\nExport current Configuration of priority. \n");
    PrintThreadPriorityFunc(&pAttr, iPlicy);

    printf("\nExport SCHED_FIFO of priority. \n");
    PrintThreadPriorityFunc(&pAttr, SCHED_FIFO);

    printf("\nExport SCHED_RR of priority. \n");
    PrintThreadPriorityFunc(&pAttr, SCHED_RR);

    printf("\nExport priority of current thread. \n");
    int iPriority = GetThreadPriorityFunc(&pAttr);
    printf("Set thread policy \n");

    printf("Set SCHED_FIFO policy\n");
    SetThreadPolicyFunc(&pAttr, SCHED_FIFO);
   
    printf("Set SCHED_RR policy\n");
    SetThreadPolicyFunc(&pAttr, SCHED_RR);
    printf("Restore current policy\n");
    SetThreadPolicyFunc(&pAttr, iPlicy);

    irs = pthread_attr_destroy(&pAttr);
    assert(irs==0);
   
    return 0;
}
